import 'package:syria_sports/data/models/error_model.dart';

import '../../core/utils/pref_utils.dart';
import '../../core/utils/show_toast.dart';
import '../apiClient/api_client.dart';
import '../apiClient/api_keys.dart';
import '../models/auth/user.dart';
import '../models/data.dart';
import '../remote/dio_helper.dart';

class UsersController{
  static Future<Data?> getAllUsers()async{
    var res = await DioHelper.getData( url: ApiClient.users, bearerToken: PrefUtils.getToken());

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showMessage(res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      return response;
    }
    return null;
  }

  static Future<Data?> addUser(User user)async{
    var res = await DioHelper.postData(
      url: ApiClient.user,
      data: await user.toJson(),
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      if(res.data["errors"]!= null){
        String errors =res.data["errors"].first;
        for(int i=1;i<res.data["errors"].length ;i++){
          errors += '\n${res.data["errors"][i]}';
        }
        showMessage(errors);
      }else{
        showMessage(res.data[ApiKeys.message]);
      }
      return Data.fromJson(res.data);
    }
    return null;
  }
  static Future<Data?> register(User user)async{
    var res = await DioHelper.postData(
      url: ApiClient.register,
      data: await user.toJson(),
    );
    if(res == null)return null;

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      print("data is ${res.data}");

      showMessage(res.data[ApiKeys.message]);
      Data response = Data.fromJson(res.data);
/*
      var user = User.fromJson(response.data);
      PrefUtils.saveUserData(user);
*/
      return response;
    }
    return null;
  }
  static Future<Data?> getUserById(int id)async{
    var res = await DioHelper.getData(
        url: ApiClient.user,
        query: {
          'id':id
        },
        bearerToken: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showMessage(res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      return response;
    }
    return null;
  }

  static Future<Data?> disableAccount(String id)async{
    var res = await DioHelper.getData(
      url: ApiClient.disableAccount,
      query: {
        'id':id
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showErrorMessage(message: res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      showMessage(res.data[ApiKeys.message]);
      return response;
    }
    return null;
  }
  static Future<Data?> activateAccount(String id)async{
    var res = await DioHelper.getData(
      url: ApiClient.activateAccount,
      query: {
        'id':id
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showErrorMessage(message: res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      showMessage(res.data[ApiKeys.message]);
      return response;
    }
    return null;
  }

  static Future<Data?> changeMyPrivacy(bool value)async{
    var res = await DioHelper.getData(
      url: ApiClient.changeMyPrivacy,
      query: {
        'public':value?1:0
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showErrorMessage(message: res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      showMessage(res.data[ApiKeys.message]);
      return response;
    }
    return null;
  }

  static Future<Data?> deleteById(String id)async{
    var res = await DioHelper.postData(
      url: ApiClient.coachDelete,
      data: {
        'id':id
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }

    return null;
  }

  static Future<Data?> updateUser(User user)async{
    var res = await DioHelper.postData(
      url: ApiClient.updateUser,
      data:await user.toJson(),
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      if(res.data[ApiKeys.message] != null) {
        showMessage(res.data[ApiKeys.message]);
      }else{
        showMessage(res.data.toString());
      }
      return Data.fromJson(res.data);
    }

    return null;
  }

}